package com.doctor.windflower_doctor.activity;

import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.doctor.windflower_doctor.R;
import com.doctor.windflower_doctor.SystemApplication;
import com.doctor.windflower_doctor.actionBeen.UserAction;
import com.doctor.windflower_doctor.db.SharedPreferencesDB;
import com.doctor.windflower_doctor.delegate.WindflowerDelegate;
import com.doctor.windflower_doctor.entity.UserBeen;
import com.doctor.windflower_doctor.http.callback.HttpRequestCallBack;
import com.doctor.windflower_doctor.http.parser.impl.JsonParser;
import com.doctor.windflower_doctor.http.request.HttpRequest;
import com.doctor.windflower_doctor.http.request.HttpRequestParams;
import com.doctor.windflower_doctor.http.response.HttpResponseInfo;
import com.doctor.windflower_doctor.pop.EditRellyNamePop;
import com.doctor.windflower_doctor.pop.TypeSelectPop;
import com.doctor.windflower_doctor.util.Constent;
import com.doctor.windflower_doctor.util.StringUtil;
import com.doctor.windflower_doctor.view.LoadingDialog;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.lidroid.xutils.util.LogUtils;
import com.nostra13.universalimageloader.core.DisplayImageOptions;

/**
 * <p>
 * 功能 用户详情页
 * </p>
 * 
 * <p>
 * Copyright 北京亚普拓信息咨询服务有限公司 2014 All right reserved.
 * </p>
 * 
 * @author 皮勉 时间 2014年10月13日 下午3:48:13
 * @version 1.0 </br> 最后修改人 无
 * 
 */
public class SickDetailActivity extends BaseActivity implements OnClickListener, Constent {
	private TextView textView;
	private ImageButton back_btn;
	private ImageButton orenge_btn;
	private ImageView icon_img;
	private TextView nick_name;
	private TextView nameText;
	private TextView age_text;
	private TextView state_text, type_select;
	private TextView oneself_sick_history;
	private TextView family_sick_history;
	private TextView pregnancy_times;
	private TextView baby_times;
	private RelativeLayout history_asking;
	private RelativeLayout record_pregnancy, edit_layout;
	private ImageView edit_img, submit_img;
	private EditText sick_edit;
	private String _id;
	private String type_name;
	private Resources resources;
	private LoadingDialog loadingDialog;
	private String userId;
	private DisplayImageOptions options;
	private String remark, condition;
	private LinearLayout full_screem;

	@Override
	protected void getSaveData(Bundle savedInstanceState) {

		// TODO Auto-generated method stub

	}

	@Override
	protected void processLogic() {

		// TODO Auto-generated method stub

	}

	@Override
	protected void setListener() {
		back_btn.setOnClickListener(this);
		orenge_btn.setOnClickListener(this);
		history_asking.setOnClickListener(this);
		record_pregnancy.setOnClickListener(this);
		edit_img.setOnClickListener(this);
		submit_img.setOnClickListener(this);
		type_select.setOnClickListener(this);
		edit_layout.setOnClickListener(this);
		full_screem.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View arg0, MotionEvent arg1) {
				if (edit_layout.getVisibility() == View.VISIBLE)
					edit_layout.setVisibility(View.GONE);
				return false;
			}
		});
	}

	@Override
	protected void findViews() {
		loadingDialog = new LoadingDialog(this);
		resources = this.getResources();
		textView = (TextView) findViewById(R.id.textView);
		textView.setText("个人详情");
		Bundle bundle = getIntent().getExtras();
		userId = bundle.getString(USERID);
		back_btn = (ImageButton) findViewById(R.id.back_btn);
		orenge_btn = (ImageButton) findViewById(R.id.orenge_btn);
		orenge_btn.setVisibility(View.VISIBLE);
		orenge_btn.setImageResource(R.drawable.more);
		icon_img = (ImageView) findViewById(R.id.icon_img);
		nick_name = (TextView) findViewById(R.id.nick_name);
		nameText = (TextView) findViewById(R.id.name);
		age_text = (TextView) findViewById(R.id.age_text);
		state_text = (TextView) findViewById(R.id.state_text);
		oneself_sick_history = (TextView) findViewById(R.id.oneself_sick_history);
		family_sick_history = (TextView) findViewById(R.id.family_sick_history);
		pregnancy_times = (TextView) findViewById(R.id.pregnancy_times);
		baby_times = (TextView) findViewById(R.id.baby_times);
		history_asking = (RelativeLayout) findViewById(R.id.history_asking);
		record_pregnancy = (RelativeLayout) findViewById(R.id.record_pregnancy);
		edit_img = (ImageView) findViewById(R.id.edit_img);
		submit_img = (ImageView) findViewById(R.id.submit_img);
		sick_edit = (EditText) findViewById(R.id.sick_edit);
		type_select = (TextView) findViewById(R.id.type_select);
		edit_layout = (RelativeLayout) findViewById(R.id.edit_layout);
		full_screem = (LinearLayout) findViewById(R.id.full_screem);
		sick_edit.setEnabled(false);
		options = new DisplayImageOptions.Builder().showImageOnLoading(R.drawable.loading_img).showImageOnFail(R.drawable.list_default_icon)
				.showImageForEmptyUri(R.drawable.list_default_icon).cacheInMemory(true).cacheOnDisk(true).bitmapConfig(Bitmap.Config.ARGB_8888).build();
		postDate();
	}

	private void initUserInfo(UserBeen userBeen) {
		SystemApplication.getInstance().mImageLoad1.displayImage(userBeen.imgUrl, icon_img, options);
		nick_name.setText(StringUtil.isEmpty(userBeen.nick) ? "用户" + userBeen.userId.subSequence(userBeen.userId.length() - 6, userBeen.userId.length())
				: userBeen.nick);
		if (!StringUtil.isEmpty(userBeen.remark))
			nameText.setText("(" + userBeen.remark + ")");
		if (!StringUtil.isEmpty(userBeen.age))
			age_text.setText(userBeen.age + "岁");
		state_text.setText(userBeen.prePeriod);
		type_select.setText(userBeen.groupName);
		oneself_sick_history.setText(userBeen.diseaseClassSingle);
		family_sick_history.setText(userBeen.diseaseClassFam);
		pregnancy_times.setText(userBeen.pregnantNum + "");
		baby_times.setText(userBeen.gnantNum + "");
		sick_edit.setText(userBeen.condition);
	}

	@Override
	public int getResLayoutId() {

		// TODO Auto-generated method stub
		return R.layout.activity_sick_detail;

	}

	@Override
	public void onClick(View v) {
		if (v.getId() != R.id.orenge_btn)
			if (edit_layout.getVisibility() == View.VISIBLE)
				edit_layout.setVisibility(View.GONE);
		switch (v.getId()) {
		case R.id.back_btn:
			finish();
			break;
		case R.id.orenge_btn:
			if (edit_layout.getVisibility() == View.VISIBLE) {
				edit_layout.setVisibility(View.GONE);
			} else {
				edit_layout.setVisibility(View.VISIBLE);
			}
			break;
		case R.id.history_asking:
			Intent intent = new Intent(SickDetailActivity.this, HistoryAskingActivity.class);
			Bundle bundle = new Bundle();
			bundle.putString(USERID, userId);
			intent.putExtras(bundle);
			startActivity(intent);
			break;
		case R.id.record_pregnancy:
			SystemApplication.getInstance().doctorId = userId;
			startActivity(new Intent(SickDetailActivity.this, PregnancyStatisticsActivity.class));
			break;
		case R.id.edit_img:
			sick_edit.setEnabled(true);
			edit_img.setVisibility(View.GONE);
			submit_img.setVisibility(View.VISIBLE);
			break;
		case R.id.submit_img:
			condition = sick_edit.getText().toString();
			if (StringUtil.isEmpty(condition)) {
				edit_img.setVisibility(View.VISIBLE);
				submit_img.setVisibility(View.GONE);
				sick_edit.setEnabled(false);
				return;
			}
			updateCondition();
			break;
		case R.id.type_select:
			TypeSelectPop typeSelectPop = new TypeSelectPop(SickDetailActivity.this, userId);
			typeSelectPop.setWindflowerDelegate(new WindflowerDelegate() {

				@Override
				public void setName(String name) {
					type_name = name;
					type_select.setText(resources.getString(R.string.nbsp) + name + resources.getString(R.string.nbsp));
				}

				@Override
				public void setId(String _id) {

				}

				@Override
				public void isOr(boolean bool) {

				}
			});
			typeSelectPop.show();
			break;
		case R.id.edit_layout:
			EditRellyNamePop editRellyNamePop = new EditRellyNamePop(SickDetailActivity.this);
			editRellyNamePop.setWindflowerDelegate(new WindflowerDelegate() {

				@Override
				public void setName(String name) {
					remark = name;
					updateRemark();
				}

				@Override
				public void setId(String _id) {

				}

				@Override
				public void isOr(boolean bool) {

				}
			});
			editRellyNamePop.show();
			break;
		}
	}

	/**
	 * <p>
	 * 功能 获取用户详细信息
	 * </p>
	 * 
	 * @author 皮勉 时间 2014年11月7日 下午2:23:11
	 */
	void postDate() {
		HttpMethod requestMethod = HttpMethod.GET;
		String requestUrl = URL + GROUP_DOCTOR_GETUSER + "?" + DOCTORID + "=" + SharedPreferencesDB.getInstance(this).getDocotorId() + "&" + USERID + "="
				+ userId;
		LogUtils.e("requestUrl>>>>>>>>>" + requestUrl);
		HttpRequestCallBack<UserAction> requestCallBack = new HttpRequestCallBack<UserAction>(new JsonParser(), UserAction.class) {
			@Override
			protected void handleResult(HttpResponseInfo<UserAction> responseInfo) {
				loadingDialog.close();
				if (responseInfo.success.success) {
					UserBeen userBeen = responseInfo.success.data;
					if (userBeen != null)
						initUserInfo(userBeen);
				}
			}

			@Override
			protected void handleError(HttpException e, String msg) {
				loadingDialog.close();
				LogUtils.e("=====getUserInfermation====3===" + msg);
			}

			@Override
			public void onStart() {
				loadingDialog.show();
				super.onStart();

			}
		};
		HttpRequestParams params = new HttpRequestParams(requestMethod, requestUrl, requestCallBack);
		HttpRequest.send(params);
	}

	/**
	 * <p>
	 * 功能 编辑备注姓名
	 * </p>
	 * 
	 * @author 皮勉 时间 2014年11月7日 下午2:23:11
	 */
	void updateRemark() {
		HttpMethod requestMethod = HttpMethod.POST;
		String requestUrl = URL + GROUP_DOCTOR_EDITREMARK;
		LogUtils.e("requestUrl>>>>>>>>>" + requestUrl);
		HttpRequestCallBack<UserAction> requestCallBack = new HttpRequestCallBack<UserAction>(new JsonParser(), UserAction.class) {
			@Override
			protected void handleResult(HttpResponseInfo<UserAction> responseInfo) {
				loadingDialog.close();
				if (responseInfo.success.success) {
					nameText.setText("(" + remark + ")");
				}
			}

			@Override
			protected void handleError(HttpException e, String msg) {
				loadingDialog.close();
				LogUtils.e("=====getUserInfermation====3===" + msg);
			}

			@Override
			public void onStart() {
				loadingDialog.show();
				super.onStart();

			}
		};
		HttpRequestParams params = new HttpRequestParams(requestMethod, requestUrl, requestCallBack);
		params.addBodyParameter(DOCTORID, SharedPreferencesDB.getInstance(this).getDocotorId());
		params.addBodyParameter(USERID, userId);
		params.addBodyParameter(REMARK, remark);
		HttpRequest.send(params);
	}

	/**
	 * <p>
	 * 功能 编辑病情备注
	 * </p>
	 * 
	 * @author 皮勉 时间 2014年11月7日 下午2:23:11
	 */
	void updateCondition() {
		HttpMethod requestMethod = HttpMethod.POST;
		String requestUrl = URL + GROUP_DOCTOR_EDITCONDITION;
		LogUtils.e("requestUrl>>>>>>>>>" + requestUrl);
		HttpRequestCallBack<UserAction> requestCallBack = new HttpRequestCallBack<UserAction>(new JsonParser(), UserAction.class) {
			@Override
			protected void handleResult(HttpResponseInfo<UserAction> responseInfo) {
				loadingDialog.close();
				if (responseInfo.success.success) {
					Toast.makeText(SickDetailActivity.this, "病情备注修改成功", Toast.LENGTH_SHORT).show();
					edit_img.setVisibility(View.VISIBLE);
					submit_img.setVisibility(View.GONE);
					sick_edit.setEnabled(false);
				}
			}

			@Override
			protected void handleError(HttpException e, String msg) {
				loadingDialog.close();
				LogUtils.e("=====getUserInfermation====3===" + msg);
			}

			@Override
			public void onStart() {
				loadingDialog.show();
				super.onStart();

			}
		};
		HttpRequestParams params = new HttpRequestParams(requestMethod, requestUrl, requestCallBack);
		params.addBodyParameter(DOCTORID, SharedPreferencesDB.getInstance(this).getDocotorId());
		params.addBodyParameter(USERID, userId);
		params.addBodyParameter(CONDITION, condition);
		HttpRequest.send(params);
	}
}
